package com.tyjc.unique.dlt;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.AnaylaseBaseFragment;
import com.tyjc.unique.net.StringResultBean_New;
import com.tyjc.unique.view.PrizeDltView;
import com.fyzk.activity.zhifu.NumLotteryZhiFuActivity;
import com.fyzk.bean.CurrentPhaseInfo;
import com.fyzk.betcode.AppLotteryCodeListManager;
import com.fyzk.betcode.BetLotteryInfo;
import com.fyzk.betcode.CodeFactory;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import http.Common.CommandUtil;

/**
 * <h1><b>功能说明</b></h1>
 * <p>从33个红球和16个篮球中机选出5注号码各不相同的注码</p></br>
 * <p>机选周期为天</p></br>
 *
 * @author miao
 */
@ContentView(R.layout.fg_ssqluckynum)
public class DLTLuckyNumFragment extends AnaylaseBaseFragment {

    private ListView luckynums_view;
    private Button refresh;
    private TextView luck_txt;
    private ArrayList<int[]> luckynums_data;

    private LuckNumAdapter adapter;


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refresh = (Button) view.findViewById(R.id.update_luckynum);
        luckynums_view =  view.findViewById(R.id.lucky_nums);
        luck_txt = (TextView) view.findViewById(R.id.lucky_txt);
        luck_txt.setText("");
        refresh.setOnClickListener(this);
        initLuckyNums();
        findCurrentPhase();
        if(MySharedPreferences.getInstance().getOpenStatus()){
            view.findViewById(R.id.lucky_nums_bet).setVisibility(View.VISIBLE);
        }else{
            view.findViewById(R.id.lucky_nums_bet).setVisibility(View.GONE);
        }
    }

    private void initLuckyNums() {
        luckynums_data = new ArrayList<>();
        randomNoRepeat(35);
        adapter = new LuckNumAdapter(getContext());
        luckynums_view.setAdapter(adapter);

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.update_luckynum:
                randomNoRepeat(35);
                adapter.notifyDataSetChanged();
                break;

            default:
                break;
        }
    }



    private void findCurrentPhase() {
        try {
            JSONObject params = new JSONObject();
            params.put("lotteryType", LotteryIDUtils.DLT_LOTTERY_ID_INT);
            httpPostJson(CommandUtil.FINDCURRENTPHASE, Commands.FINDCURRENTPHASE, params, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean_New resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode){
            case CommandUtil.FINDCURRENTPHASE:
                CurrentPhaseInfo info = JSON.parseObject(resultBean.getData(),
                        CurrentPhaseInfo.class);
                AppLotteryInfos.setCurrentPrizeInfo(info);
                break;
        }
    }

    public void randomNoRepeat(int max) {
        luckynums_data.clear();
        ArrayList<Integer> allNumArr = new ArrayList<Integer>();
        ArrayList<Integer> blueArr = new ArrayList<Integer>();
        for (int i = 1; i < max + 1; i++) {
            allNumArr.add(i);
        }
        for (int x = 0; x < 5; x++) {
            int[] arr = new int[5];
            ArrayList<Integer> randomArr = new ArrayList<Integer>();
            for (int i = 0; i < 5; i++) {
                int j = (int) (Math.random() * allNumArr.size());//机选出5个为主信息
                while (randomArr.contains(allNumArr.get(j))) {
                    j = (int) (Math.random() * allNumArr.size());
                }
                randomArr.add(allNumArr.get(j));
                arr[i] = allNumArr.get(j);
                allNumArr.remove(j);
            }
            Arrays.sort(arr);
            int[] arrs = new int[7];
            for (int z = 0; z < 7; z++) {
                if (z < 5) {
                    arrs[z] = arr[z];
                } else {
                    arrs[z] = (int) (Math.random() * 12) + 1;
                    while (blueArr.contains((Integer) arrs[z])) {
                        arrs[z] = (int) (Math.random() * 12) + 1;
                    }
                    blueArr.add(arrs[z]);
                }
            }
            luckynums_data.add(arrs);
        }
    }


    @Event(type = View.OnClickListener.class, value = {R.id.lucky_nums_bet})
    private void bet(View view) {
        BetLotteryInfo betInfo = null;
        for (int[] numA : luckynums_data) {
            StringBuffer buffer = new StringBuffer();
            StringBuffer buffer_show = new StringBuffer();
//            buffer.append(LotteryIDUtils.SSQ_LOTTERY_COMMON_DAN_ID_INT);
//            buffer.append("-");

            for (int i = 0; i < numA.length; i++) {
                int num = numA[i];
                String numStr = "" + num;
                if (num <= 9) {
                    numStr = "0" + num;
                }
                buffer.append(numStr);
                buffer_show.append(numStr);
                if (i == 4) {
                    buffer.append("|");
                    buffer_show.append("|");
                } else if (i < 6) {
                    buffer.append(",");
                    buffer_show.append(",");
                }
            }

            AppLotteryCodeListManager.getInstance().AddLotteryInfo(LotteryIDUtils
                            .DLT_LOTTERY_COMMON_DAN_ID_INT,
                    buffer.toString(), CodeFactory.getCode(LotteryIDUtils.DLT_LOTTERY_ID_INT)
                            .formatHtmlCode2(buffer.toString()), 1, null, 0);
            betInfo = new BetLotteryInfo(LotteryIDUtils.DLT_LOTTERY_COMMON_DAN_ID_INT, buffer
                    .toString(), buffer_show.toString(),
                    1, null, 0);
        }

        Intent intent = new Intent(getContext(), NumLotteryZhiFuActivity.class);
        intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.DLT_LOTTERY_ID_INT);
        intent.putExtra("bet", (Serializable) betInfo);
        startActivity(intent);


    }


    public class LuckNumAdapter extends BaseAdapter {

        private Context context;

        public LuckNumAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return luckynums_data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PrizeDltView dltView;
            if (convertView == null) {
                dltView = new PrizeDltView(context);
                convertView = dltView;
            } else {
                dltView = (PrizeDltView) convertView;
            }
            dltView.setNum(luckynums_data.get(position));
            return convertView;
        }

    }

    @Override
    public void onDestroy() {
        AppLotteryCodeListManager.getInstance().getList().clear();
        AppLotteryCodeListManager.betType = AppLotteryCodeListManager.FIRSTENTER;
        AppLotteryCodeListManager.chooseId = 0;
        super.onDestroy();
    }
}
